#include "HttpFixedResponse.h"
#include "Http.h"
#include "HttpResponse.h"
#include "Server.h"

void set_response_success(Response *response, char *msg) {
    response->staus        = 200;
    response->staus_title  = HTTP_STATUS_CODE_200;
    response->content_type = MIME_TYPE_JSON;
    sds_cat_printf(response->body, "{\"code\":1,\"msg\":\"%s\"}", msg);
    response->content_length = sds_len(response->body);
}

void set_response_success_with_data(Response *response, char *msg, char *data) {
    response->staus        = 200;
    response->staus_title  = HTTP_STATUS_CODE_200;
    response->content_type = MIME_TYPE_JSON;
    sds_cat_printf(response->body, "{\"code\":1,\"msg\":\"%s\",\"data\":\"%s\"}", msg, data);
    response->content_length = sds_len(response->body);
}

void set_response_unauthorized(Response *response) {
    response->staus        = 401;
    response->staus_title  = HTTP_STATUS_CODE_401;
    response->content_type = MIME_TYPE_JSON;
    sds_cat_len(response->body, "{\"code\":0,\"msg\":\"Unauthorized\"}", 31);
    response->content_length = 31;
}

void set_response_not_found(Response *response) {
    response->staus        = 404;
    response->staus_title  = HTTP_STATUS_CODE_404;
    response->content_type = MIME_TYPE_JSON;
    sds_cat_len(response->body, "{\"code\":0,\"msg\":\"Not found\"}", 28);
    response->content_length = 28;
}

void set_response_bad_request(Response *response, char *msg) {
    response->staus        = 400;
    response->staus_title  = HTTP_STATUS_CODE_400;
    response->content_type = MIME_TYPE_JSON;
    sds_cat_printf(response->body, "{\"code\":0,\"msg\":\"%s\"}", msg);
    response->content_length = sds_len(response->body);
}

void set_response_internal_error_default(Response *response) {
    response->staus        = 500;
    response->staus_title  = HTTP_STATUS_CODE_500;
    response->content_type = MIME_TYPE_JSON;
    sds_cat_len(response->body, "{\"code\":0,\"msg\":\"Internal error\"}", 33);
    response->content_length = 33;
}

void set_response_internal_error(Response *response, char *msg) {
    response->staus        = 500;
    response->staus_title  = HTTP_STATUS_CODE_500;
    response->content_type = MIME_TYPE_JSON;
    sds_cat_printf(response->body, "{\"code\":0,\"msg\":\"%s\"}", msg);
    response->content_length = sds_len(response->body);
}

/**
 * 一般用于身份验证
 */
void send_response_unauthorized(EventLoop *el, Client *client) {
    net_delete_file_event(el, client->fd, YX_NET_READABLE);
    set_response_unauthorized(client->response);
    generate_response(client);
    net_create_file_event(el, client->fd, YX_NET_WRITABLE, send_response, client);
    client->request->state = HTTP_REQUEST_STATE_PARSE_COMPLETED;
}

/**
 * 一般用于解析协议时出错时，直接返回错误信息
 */
void send_response_bad_request(EventLoop *el, Client *client, char *msg) {
    net_delete_file_event(el, client->fd, YX_NET_READABLE);
    set_response_bad_request(client->response, msg);
    generate_response(client);
    net_create_file_event(el, client->fd, YX_NET_WRITABLE, send_response, client);
    client->request->state = HTTP_REQUEST_STATE_PARSE_COMPLETED;
}